#!/bin/bash

./launch.sh -bg

export DISPLAY=":0.0"

rm -rf concordion_report

concordion_folder_runner -o ./concordion_report ./functional
result=$?

pkill -P `cat extremeKanban/server.pid`
rm extremeKanban/server.pid

SCREENSHOT_DIR=concordion_report/screenshots

rm -rf $SCREENSHOT_DIR
mkdir $SCREENSHOT_DIR

FILES=`rgrep -l {IMG:.*} concordion_report/`
for FILE in $FILES
do
	echo "Treating file $FILE"
	IMAGES=`grep -o {IMG:.*} $FILE`
	for IMAGE in $IMAGES
	do
		echo "	Treating image $IMAGE"
		CLEAN_IMAGE=${IMAGE:5: 56}
		DIR=`dirname $FILE`
		cp $CLEAN_IMAGE $DIR
		NEW_IMAGE="${CLEAN_IMAGE:5}"
		MINI_IMAGE=mini_$NEW_IMAGE
		convert -resize 320x320 $DIR/$NEW_IMAGE $DIR/$MINI_IMAGE
		RESULT="<a href='$NEW_IMAGE' rel='lightbox[screenshots]'><img src='$MINI_IMAGE' /></a>"
		echo "Replacing $IMAGE by $RESULT in $FILE"
		SUB_STR="s#$IMAGE#$RESULT#g"
		sed -i "$SUB_STR" $FILE
	done
done

cp functional/index.html concordion_report
cp -r functional/js functional/css functional/images concordion_report 

exit $result